package cn.wolfcode.luowowo.website.controller;


import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRegistService;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.TravelCommentQuery;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import cn.wolfcode.luowowo.website.util.UMEditorUploader;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/*
 *
 * 前端旅游日记界面
 *
 *
 * */
@Controller
@RequestMapping("travel")
public class TravelController {

    @Value("${file.path}")
    private String path;


    @Reference
    private ITravelService travelService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IUserInfoRegistService userInfoRegistService;

    @Reference
    private ITravelCommentService commentService;


    //游记列表
    @RequestMapping("")
    public Object travels(Model model, @ModelAttribute("qo") TravelQuery qo) {
        model.addAttribute("pageInfo", travelService.query(qo));
        return "travel/list";
    }


    //input 写游记
    @RequireLogin //要登录才能访问
    @RequestMapping("input")
    public Object input(Model model,Long id) {

        //目的地
        List<Destination> list = destinationService.list();
        model.addAttribute("dests", list);

        if (id!=null){
            Travel detail = travelService.get(id);
            TravelContent content = travelService.getContent(id);
            detail.setTravelContent(content);
            model.addAttribute("tv",detail);
        }

        return "travel/input";
    }


    //游记新增 修改
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Travel travel, @UserParam UserInfo userInfo) {


        if (userInfo == null) {
            throw new LogicException("请登录");
        }
        travel.setAuthor(userInfo);
        Long id = travelService.saveOrUpdate(travel);
        return AjaxResult.SUCCESS.addData(id);
    }


    //detail 点击去 游记详情
    @RequestMapping("detail")
    public Object detail(Long id, Model model) {

        Travel detail = travelService.get(id);
        TravelContent content = travelService.getContent(id);
        detail.setTravelContent(content);
        model.addAttribute("detail",detail);

        //吐司
        List<Destination> toasts = destinationService.getToasts(detail.getDest().getId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);

        //sds
        List<StrategyDetail> list = strategyDetailService.selectStrategyDetailByDeptId(detail.getDest().getId());
        model.addAttribute("sds",list);

        //ts
        List<Travel> travelList = travelService.queryViewNumTop3(detail.getDest().getId());
        model.addAttribute("ts",travelList);

        //评论列表
        TravelCommentQuery qo = new TravelCommentQuery();
        qo.setTravelId(id);
        qo.setPageSize(Integer.MAX_VALUE);
        Page page = commentService.query(qo);
        model.addAttribute("list",page.getContent());



        return "travel/detail";
    }


    //上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception {
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile, path);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName() + "\", \"size\": " + up.getSize()
                + ", \"state\": \"" + up.getState() + "\", \"type\": \"" + up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";
        result = result.replaceAll("\\\\", "\\\\");
        if (callback == null) {
            return result;
        } else {
            return "<script>" + callback + "(" + result + ")</script>";
        }
    }


    //commentAdd 评论
    @RequestMapping("commentAdd")
    public String commentAdd(Model model, @UserParam UserInfo userInfo, TravelComment comment){

//        if (userInfo==null){
//            throw new LogicException("请登录");
//        }
        comment.setUserId(userInfo.getId());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setUsername(userInfo.getNickname());
        comment.setCity(userInfo.getCity());
        TravelComment travelComment = commentService.saveOrUpdate(comment);
        model.addAttribute("c",travelComment);
        model.addAttribute("floor",commentService.getCount(comment.getTravelId()));
        return "travel/commentTpl";
    }








}
